//
//  RunesViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RunesViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "ToolsModel.h"
#import "ToolsView.h"
#import "RunesModel.h"
#import "RunnesCell.h"
#import "DetailCollectionCell.h"
#import "DOPDropDownMenu.h"

@interface RunesViewController ()<DOPDropDownMenuDataSource,DOPDropDownMenuDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)ToolsView *toolsView;
@property(nonatomic, retain)UITableView *runseTableView;

@property(nonatomic, retain)NSMutableArray *transformArr;  // 传输数组
@property(nonatomic, retain)NSMutableArray *runesArr;
@property(nonatomic, retain)NSMutableArray *redArr;
@property(nonatomic, retain)NSMutableArray *blueArr;
@property(nonatomic, retain)NSMutableArray *yellowArr;
@property(nonatomic, retain)NSMutableArray *purpleArr;

@property(nonatomic, retain)NSArray *runesLev;     // 等级
@property(nonatomic, retain)NSArray *runesColor;   // 类型
@property(nonatomic, assign)NSInteger flag;
@property (nonatomic, assign)NSInteger lev;
@end

@implementation RunesViewController

- (void)dealloc
{
    
    [_toolsView release];
    [_runseTableView release];
    [_redArr release];
    [_blueArr release];
    [_yellowArr release];
    [_purpleArr release];
    [_transformArr release];
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"符文";
        self.transformArr = [NSMutableArray array];
        self.runesArr = [NSMutableArray array];  // 符文总数组
        self.redArr = [NSMutableArray array];    // 印记数组
        self.blueArr = [NSMutableArray array];   // 雕纹数组
        self.yellowArr = [NSMutableArray array]; // 符文数组
        self.purpleArr = [NSMutableArray array]; // 精华数组
        self.lev = 0;
        self.flag = 1;
    }
    return self;
}

// 解析数据:
- (void)runesData
{
    AFHTTPRequestOperationManager *managerRunes = [AFHTTPRequestOperationManager manager];
    [managerRunes GET:@"http://lolbox.duowan.com/phone/apiRunes.php?OSType=iOS7.1.1&v=70" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *runesDic = responseObject;
        NSMutableArray *redArr = runesDic[@"Red"];
        NSMutableArray *blueArr = runesDic[@"Blue"];
        NSMutableArray *yellowArr = runesDic[@"Yellow"];
        NSMutableArray *purpleArr = runesDic[@"Purple"];
        // 印记解析
        for (NSMutableDictionary *dic in redArr) {
            RunesModel *runesModel = [[RunesModel alloc] init];
            [runesModel setValuesForKeysWithDictionary:dic];
            [self.redArr addObject:runesModel];

            [runesModel release];
        }
        // 雕纹解析
        for (NSMutableDictionary *dic in blueArr) {
            RunesModel *runesModel = [[RunesModel alloc] init];
            [runesModel setValuesForKeysWithDictionary:dic];
            [self.blueArr addObject:runesModel];

            [runesModel release];
        }
        // 符文解析
        for (NSMutableDictionary *dic in yellowArr) {
            RunesModel *runesModel = [[RunesModel alloc] init];
            [runesModel setValuesForKeysWithDictionary:dic];
            [self.yellowArr addObject:runesModel];

            [runesModel release];
        }
        // 精华解析
        for (NSMutableDictionary *dic in purpleArr) {
            RunesModel *runesModel = [[RunesModel alloc] init];
            [runesModel setValuesForKeysWithDictionary:dic];
            [self.purpleArr addObject:runesModel];

            [runesModel release];
        }
        self.runesArr = [NSMutableArray arrayWithObjects:self.redArr,self.blueArr,self.yellowArr,self.purpleArr, nil];
        [self cr];
        [self.runseTableView  reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self runesData];
}
- (void)cr
{
    // 铺设自定义View
    self.toolsView = [[ToolsView alloc] initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT)];
    self.toolsView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.toolsView];
    [_toolsView release];
    
    // 铺设tableView
    self.runseTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, VIEWWIDTH, VIEWHEIGHT - 44 ) style:UITableViewStylePlain];
    self.runseTableView.delegate = self;
    self.runseTableView.dataSource = self;
    self.runseTableView.rowHeight = 64;
    [self.toolsView addSubview:self.runseTableView];
    [_runseTableView release];
    
    // 筛选条件框
    self.runesLev = @[@"1级", @"2级", @"3级"];
    self.runesColor = @[@"印记", @"雕文", @"符文", @"精华"];
    
    // 添加下拉菜单
    DOPDropDownMenu *menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 0) andHeight:44];
    menu.dataSource = self;
    menu.delegate = self;
    [self.toolsView addSubview:menu];
    [menu release];
}
// tableview两个协议方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [self.runesArr[self.lev] count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *reuse = @"reuse";
    RunnesCell *runesCell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!runesCell) {
        runesCell = [[[RunnesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    NSArray *arr = self.runesArr[self.lev];
    RunesModel *runesModel = arr[indexPath.row];
    runesCell.runesNameLabel.text = runesModel.name;

    NSString *png = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/runes/%@_%ld.png", runesModel.img, self.flag];

    NSURL *url = [NSURL URLWithString:png];
    [runesCell.runesImag sd_setImageWithURL:url];
    
    // 改变cell的颜色
    runesCell.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1];
    
    //通过RGB来定义celld点击后的颜色
    runesCell.selectedBackgroundView = [[[UIView alloc] initWithFrame:runesCell.frame] autorelease];
    runesCell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    
    return runesCell;
    
    
}

#pragma mark 自定义菜单
// 下拉菜单横排组成个数

- (NSInteger )numberOfColumnsInMenu:(DOPDropDownMenu *)menu
{
    
    return 2;
    
}

// 子菜单个数
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column
{
    if (column == 0) {
        return self.runesLev.count;
    }else {
        return self.runesColor.count;
    }
}

// 子菜单是否含有子项目 无返回0
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfItemsInRow:(NSInteger)row column:(NSInteger)column
{
    return 0;
}

// 子项目标题
- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if (indexPath.column == 0) {
        return self.runesLev[indexPath.row];
    } else {
        return self.runesColor[indexPath.row];
    }
}

// 子菜单是否含有子项目名称 无返回nil
- (NSString *)menu:(DOPDropDownMenu *)menu titleForItemsInRowAtIndexPath:(DOPIndexPath *)indexPath
{
    return nil;
}

// 点击事件
- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath
{
//
    if (indexPath.column == 0) {
        NSInteger num = indexPath.row;

        self.flag = num + 1;
        [self.runseTableView reloadData];
        }
    
    if (indexPath.column == 1) {
        
        NSInteger num = indexPath.row;

        self.lev = num;
         [self.runseTableView reloadData];
    }
    
}

// tableview点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSArray *arr = self.runesArr[self.lev];
    RunesModel *runesModel = arr[indexPath.row];

    
    
    
    if (_flag == 1) {
        NSString *lev1 = runesModel.lev1;
        NSString *money = runesModel.iplev1;
        NSString *prop = runesModel.prop;
        NSString *temp = [NSString stringWithFormat:@"类型:%@\n金币:%@\n属性:%@", prop, money, lev1];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:temp message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }else if (_flag == 2) {
        NSString *lev2 = runesModel.lev2;
        NSString *money = runesModel.iplev2;
        NSString *prop = runesModel.prop;
        NSString *temp = [NSString stringWithFormat:@"类型:%@\n金币:%@\n属性:%@", prop, money, lev2];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:temp message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }else if (_flag == 3) {
        NSString *lev3 = runesModel.lev3;
        NSString *money = runesModel.iplev3;
        NSString *prop = runesModel.prop;
        NSString *temp = [NSString stringWithFormat:@"类型:%@\n金币:%@\n属性:%@", prop, money, lev3];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:temp message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
